package com.hospital.warehouse.drugpurchase.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hospital.util.DTO;
import com.hospital.warehouse.drugdicts.entity.Drugdicts;
import com.hospital.warehouse.drugdicts.service.DrugdictsService;
import com.hospital.warehouse.drugpurchase.entity.Drugpurchase;
import com.hospital.warehouse.drugpurchase.service.DrugpurchaseService;
import com.hospital.warehouse.exception.WarehouseException;
import com.hospital.warehouse.sys_user.entity.Sys_user;
import com.hospital.warehouse.sys_user.service.Sys_userService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Mr. YR
 * @since 2020-04-21
 */
@Controller
@RequestMapping("/warehouse/drugpurchase")
public class DrugpurchaseController {

	@Autowired
	DrugpurchaseService drugpurchaseService;
	@Autowired
	DrugdictsService drugdictsService;
	@Autowired
	Sys_userService sys_userService;

	@ResponseBody
	@RequestMapping("/paging")
	public DTO paging(Drugpurchase dp, Integer page, Integer limit) {
		DTO dto = drugpurchaseService.paging(dp, page, limit);
		return dto;
	}

	@RequestMapping("/page")
	public String page(Model model) {
		List<Drugdicts> drugs = drugdictsService.list();
		List<Sys_user> users = sys_userService.list();

		model.addAttribute("drugs", drugs);
		model.addAttribute("users", users);

		return "views/warehouse/drugpurchase.html";
	}

	@RequestMapping("/editpage")
	public String editpage(Model model, Integer dp_id) {
		if (dp_id != null) {
			Drugpurchase dp = drugpurchaseService.queryById(dp_id);
			model.addAttribute("dp", dp);
		}

		List<Drugdicts> list = drugdictsService.list();
		model.addAttribute("drugs", list);
		return "views/warehouse/drugpurchase_edit.html";
	}

	@ResponseBody
	@RequestMapping("/insertone")
	public Object insertOne(@RequestBody Drugpurchase dp, HttpSession session) {
		try {
			drugpurchaseService.saveOne(dp, session);
		} catch (WarehouseException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护";
		}
		return "新增成功";
	}

	@ResponseBody
	@RequestMapping("/delete")
	public Object delete(@RequestParam("ids[]") Integer[] ids) {
		try {
//			System.out.println(Arrays.asList(ids));
			drugpurchaseService.removeByIds(Arrays.asList(ids));
		} catch (WarehouseException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护";
		}
		return "删除成功";
	}

	@ResponseBody
	@RequestMapping("/updateone")
	public Object updateOne(@RequestBody Drugpurchase dp) {
		try {
			drugpurchaseService.updateOne(dp);
		} catch (WarehouseException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护";
		}
		return "修改成功";
	}

	@ResponseBody
	@RequestMapping("/updatestate")
	public Object updateState(Integer id, String state, HttpSession session) {
		try {
			drugpurchaseService.updateState(id, state, session);
		} catch (WarehouseException e) {
			return e.getMessage();
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护";
		}
		return "修改成功";
	}

}
